﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraBars;

namespace PRE.Catalog
{
    public partial class FrmReceipt : PRE.Catalog.FrmBase
    {
        private const string STR_ADD = "Thêm thanh toán";
        private const string STR_EDIT = "Sửa thanh toán";
        private const string STR_DELETE = "Xoá thanh toán";

        private const string STR_SELECT = "Chọn dữ liệu!";
        private const string STR_CONFIRM = "Có xoá thanh toán '{0}' không?";
        private const string STR_UNDELETE = "Không xoá được!\nDữ liệu đang được sử dụng.";
        private const string STR_DUPLICATE = "Tên này có rồi";
        private const string STR_EMPTY = "Chưa nhập [{0}]";

        new BLL.ReceiptBLL _bll;

        public FrmReceipt()
        {
            InitializeComponent();

            bbiAdd.Visibility = BarItemVisibility.Never;
            bbiEdit.Visibility = BarItemVisibility.Never;
            bbiDelete.Visibility = BarItemVisibility.Never;
            bbiSave.Visibility = BarItemVisibility.Never;
            bbiCancel.Visibility = BarItemVisibility.Never;
            bbiPrint.Visibility = BarItemVisibility.Always;

            grvMain.OptionsBehavior.Editable = false;
            _bll = new BLL.ReceiptBLL();

            rdgSelect_SelectedIndexChanged(null, null);
        }

        #region Override
        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            //var tmp = grvMain.GetFocusedRowCellValue("Id") + "";
            //long id = UTL.Number.ToInt64(tmp);

            //if (id == 0) UTL.Message.Show(STR_SELECT, STR_DELETE);
            //else
            //{
            //    var cfm = String.Format(STR_CONFIRM, txtName.Text);
            //    var oki = UTL.Message.Show(cfm, STR_DELETE, MessageBoxButtons.OKCancel);

            //    if (oki == DialogResult.OK)
            //        if (_bll.Delete(id) == UTL.Session.PerState.Success) PerformRefresh();
            //        else UTL.Message.Show(STR_UNDELETE, STR_DELETE);
            //}

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            base.PerformEdit();
        }

        protected override void PerformFind()
        {
            base.PerformFind();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            var frm = new UTL.Forms.FrmPrint();
            frm.Text = "In: " + Text;

            var rpt = new Report.Rpt_Receipt();
            rpt.DataSource = _dtb;
            frm.SetReport(rpt);

            frm.MdiParent = MdiParent;
            frm.Show();
            frm.Activate();

            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            LoadData();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            switch (_frmState)
            {
                case UTL.Session.FormState.Add:
                    if (InsertObject())
                    {
                        ResetText(); LoadData();
                    }
                    break;

                case UTL.Session.FormState.Edit:
                    if (UpdateObject())
                    {
                        ChangeStatus(); ReadOnlyControl();
                        PerformRefresh();
                    }
                    break;
            }

            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            //txtName.Text = null;
            //txtNumbeds.Text = "0";
            //txtCapacity.Text = "0";
            //txtDescript.Text = null;

            //lokStatus.ItemIndex = 0; // none
            //lokRoomType.ItemIndex = 0; // none

            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            //txtName.DataBindings.Clear();
            //txtNumbeds.DataBindings.Clear();
            //txtCapacity.DataBindings.Clear();
            //txtDescript.DataBindings.Clear();
            //lokStatus.DataBindings.Clear();
            //lokRoomType.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            //txtName.DataBindings.Add("EditValue", _dtb, ".Name");
            //lokRoomType.DataBindings.Add("EditValue", _dtb, ".TypeId");
            //lokStatus.DataBindings.Add("EditValue", _dtb, ".StatusId");
            //txtCapacity.DataBindings.Add("EditValue", _dtb, ".Capacity");
            //txtDescript.DataBindings.Add("EditValue", _dtb, ".Descript");
            //txtNumbeds.DataBindings.Add("EditValue", _dtb, ".Numbeds");

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtName.Properties.ReadOnly = isReadOnly;
            //txtNumbeds.Properties.ReadOnly = isReadOnly;
            //txtCapacity.Properties.ReadOnly = isReadOnly;
            //txtDescript.Properties.ReadOnly = isReadOnly;
            //lokStatus.Properties.ReadOnly = isReadOnly;
            //lokRoomType.Properties.ReadOnly = isReadOnly;

            //grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            try
            {
                if (!ValidInput()) return false;

                //var tmp = grvMain.GetFocusedRowCellValue("Id") + "";
                //long id = UTL.Number.ToInt64(tmp);

                var o = new DAL.tb_Room()
                {
                    //Id = id,
                    //Name = txtName.Text,
                    //StatusId = UTL.Number.ToInt64(lokStatus.EditValue + ""),
                    //TypeId = UTL.Number.ToInt64(lokRoomType.EditValue + ""),
                    //Numbeds = UTL.Number.ToInt32(txtNumbeds.Text),
                    //Descript = txtDescript.Text,
                    //Capacity = UTL.Number.ToInt32(txtCapacity.Text)
                };

                var oki = _bll.Update(o);
                if (oki == UTL.Session.PerState.Duplicate)
                    UTL.Message.Show(STR_DUPLICATE, STR_EDIT);
                return oki == UTL.Session.PerState.Success ? true : false;
            }
            catch { return false; }
        }

        /// <summary>
        /// Delete object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            try
            {
                if (!ValidInput()) return false;

                var o = new DAL.tb_Room()
                {
                    //Name = txtName.Text,
                    //StatusId = UTL.Number.ToInt64(lokStatus.EditValue + ""),
                    //TypeId = UTL.Number.ToInt64(lokRoomType.EditValue + ""),
                    //Numbeds = UTL.Number.ToInt32(txtNumbeds.Text),
                    //Descript = txtDescript.Text,
                    //Capacity = UTL.Number.ToInt32(txtCapacity.Text)
                };

                var oki = _bll.Insert(o);
                if (oki == UTL.Session.PerState.Duplicate)
                    UTL.Message.Show(STR_DUPLICATE, STR_ADD);
                return oki == UTL.Session.PerState.Success ? true : false;
            }
            catch { return false; }
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            if (Convert.ToBoolean(rdgSelect.EditValue))
                _dtb = _bll.Select(UTL.TimeDate.GetStartOfDay(DAL.BaseDAL.CurrentTime.Value),
                UTL.TimeDate.GetEndOfDay(DAL.BaseDAL.CurrentTime.Value));
            else
                _dtb = _bll.Select(UTL.TimeDate.GetStartOfDay(dteFrom.DateTime),
                    UTL.TimeDate.GetEndOfDay(dteTo.DateTime));

            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
                gridColumn2.BestFit(); // fit column
            }

            base.LoadData();
        }

        /// <summary>
        /// Valid data before insert or update to database
        /// </summary>
        /// <returns>True if valid else false</returns>
        protected override bool ValidInput()
        {
            //if (txtName.Text + "" == "")
            //{
            //    UTL.Message.Show(String.Format(STR_EMPTY, lblName.Text), STR_ADD);
            //    txtName.Focus();
            //    return false;
            //}

            //if (UTL.Number.ToInt32(txtCapacity.Text + "") == 0)
            //{
            //    UTL.Message.Show(String.Format(STR_EMPTY, lblCapacity.Text), STR_ADD);
            //    txtCapacity.Focus();
            //    return false;
            //}

            return base.ValidInput();
        }
        #endregion

        private void rdgSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            var x = Convert.ToBoolean(rdgSelect.EditValue);
            dteFrom.Properties.ReadOnly = x;
            dteTo.Properties.ReadOnly = x;
            cbeMonth.Properties.ReadOnly = x;
            cbeQuater.Properties.ReadOnly = x;

            LoadData();
        }

        private void dteFrom_EditValueChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void dteTo_EditValueChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void cbeMonth_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void cbeQuater_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}